

package com.wangwen.wechatmp.exception;

import com.wangwen.wechatmp.entity.ApiResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * 全局异常处理器
 *
 * @author wangwei
 */
@RestControllerAdvice
public class ExceptionHandler {
	private Logger logger = LoggerFactory.getLogger(getClass());


	/**
	 * 处理自定义异常  数据检查
	 */
	@org.springframework.web.bind.annotation.ExceptionHandler(RuntimeCheckException.class)
	public ApiResponse handleRRException(RuntimeCheckException e){
		ApiResponse r = new ApiResponse();
		r.setCode(e.getCode());
		r.setMessage(e.getMessage());

		return r;
	}

	@org.springframework.web.bind.annotation.ExceptionHandler(NoHandlerFoundException.class)
	public ApiResponse handlerNoFoundException(Exception e) {
		logger.error(e.getMessage(), e);
		return ApiResponse.fail("路径不存在，请检查路径是否正确");
	}

	@org.springframework.web.bind.annotation.ExceptionHandler(DuplicateKeyException.class)
	public ApiResponse handleDuplicateKeyException(DuplicateKeyException e){
		logger.error(e.getMessage(), e);
		return ApiResponse.fail("信息已存在,请检查重试!");
	}

	@org.springframework.web.bind.annotation.ExceptionHandler(Exception.class)
	public ApiResponse handleException(Exception e){
		logger.error(e.getMessage(), e);
		return ApiResponse.error();
	}
}
